package com;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;

public class GUI extends JFrame {

    public int rows;
    public int cols;
    private JPanel gridPanel = new JPanel();
    private JPanel otherPanel = new JPanel();
    private JButton[][] gridBtn;
    private JButton oneButton;
    private JTextField timeSetting;
    private int widthWindow = 640;
    private int heightWindow = 750;
    private int cellSizeRow;
    private int cellSizeCol;
    private GameMap tempGameMap;
    
    public GUI(GameMap gMap){
        super("生命游戏");
        setBounds(100,200,widthWindow,heightWindow);//边界
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认退出模式
        setLayout(new BorderLayout());
 
        //根据gameMap设置界面显示行列
        rows = gMap.getRows();
        cols = gMap.getCols();
        gridBtn = new JButton[rows][cols];
        cellSizeRow = widthWindow/rows;
        cellSizeCol = widthWindow/cols;
        tempGameMap = gMap;
        
        gridPanel.setBounds(new Rectangle(650, 650));
        gridPanel.setLayout(new GridLayout(rows,cols));
       
        for(int i= 0;i<rows; i++){
            for(int j= 0; j<cols;j++){
                oneButton = new JButton();
                oneButton.addMouseListener(new ChangeColor());
                oneButton.setPreferredSize(new Dimension(cellSizeRow,cellSizeCol));

                gridBtn[i][j] = oneButton;
                gridPanel.add(gridBtn[i][j]);
            }
        }
        otherPanel.setBounds(new Rectangle(650,50));

        timeSetting = new JTextField("输入间隔时间");
        timeSetting.setBounds(10,300,50,25);
        otherPanel.add(timeSetting);

        oneButton = new JButton("确认");
        oneButton.addActionListener(new ChangeInterval());
        oneButton.setBounds(50,215,10,10);
        otherPanel.add(oneButton);

        oneButton = new JButton("暂停");
        oneButton.addActionListener(new Pause());
        oneButton.setBounds(50,425,10,10);
        otherPanel.add(oneButton);

        this.add(gridPanel,BorderLayout.NORTH);
        this.add(otherPanel,BorderLayout.SOUTH);

        setVisible(true);
    }

    public void showGrid(){

    	  int rows = tempGameMap.getRows();
        int cols= tempGameMap.getCols();

        //gridPanel.repaint();

        for(int i=0; i<rows; i++){
            for(int j=0; j<cols; j++){
                if (tempGameMap.get(i, j) == true){
                    gridBtn[i][j].setBackground(Color.WHITE);
                }
                else{
                    gridBtn[i][j].setBackground(Color.BLACK);
                }
                //gridPanel.add(gridBtn[i][j]);
            }
        }
        //this.add(gridPanel);
    }

    private class ChangeInterval implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){

            int itv = 0;

            try{
                itv = Integer.parseInt(timeSetting.getText().trim());
            }catch (Exception e){
                JOptionPane.showMessageDialog(null, "请输入有效数字");
            }

            GameTimer.interval = itv;//修改类常量            
        }
    }

    private class Pause implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0){

        	GameTimer.isGoOn = !GameTimer.isGoOn;
        	
        }
    }

    private class ChangeColor implements MouseListener{
   		@Override
		public void mouseClicked(MouseEvent arg0) {
			
		}
   		public void mouseDragged(MouseEvent e){
   			mouseClicked(e);
   		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			int cellX = e.getX();
			int cellY = e.getY();
			boolean val=true;
			
			cellX/=cellSizeRow;
			cellY/=cellSizeCol;
			val = tempGameMap.get(cellX, cellY);
			
			tempGameMap.set(cellX, cellY, !val);
			showGrid();
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
    }
}
